package com.yphone.tools;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.File;
import java.security.MessageDigest;

public class GlideManager {

    public static class AutoRotateTransformation extends BitmapTransformation {
        private float rotateRotationAngle = 0.0f;

        public AutoRotateTransformation(Context context, float rotateRotationAngle2) {
            super(context);
            this.rotateRotationAngle = rotateRotationAngle2;
        }

        /* access modifiers changed from: protected */
        public Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            if (this.rotateRotationAngle == 0.0f || toTransform.getWidth() <= toTransform.getHeight()) {
                return toTransform;
            }
            Matrix matrix = new Matrix();
            matrix.postRotate(this.rotateRotationAngle);
            return Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true);
        }

        public String getId() {
            return "rotate" + this.rotateRotationAngle;
        }

    }

    public static class AutoRotateTransformationH extends BitmapTransformation {
        private float rotateRotationAngle = 0.0f;

        public AutoRotateTransformationH(Context context, float rotateRotationAngle2) {
            super(context);
            this.rotateRotationAngle = rotateRotationAngle2;
        }

        /* access modifiers changed from: protected */
        public Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            if (this.rotateRotationAngle == 0.0f || toTransform.getWidth() >= toTransform.getHeight()) {
                return toTransform;
            }
            Matrix matrix = new Matrix();
            matrix.postRotate(this.rotateRotationAngle);
            return Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true);
        }

        public String getId() {
            return "rotate" + this.rotateRotationAngle;
        }

    }

    public static void glide(Context context, ImageView iv, String path, int defualtIco) {
        if (path == null) {
            Glide.with(context).load("").asBitmap().placeholder(defualtIco).into(iv);
        } else {
            Glide.with(context).load(path.trim()).asBitmap().placeholder(defualtIco).into(iv);
        }
    }

    public static void glide(Context context, ImageView iv, String path) {
        if (!TextUtils.isEmpty(path)) {
            Glide.with(context).load(path.trim()).asBitmap().into(iv);
        }
    }

    public static void glide(Context context, ImageView iv, byte[] imageBytes, BitmapTransformation... transformations) {
        Glide.with(context).load(imageBytes).transform(transformations).into(iv);
    }

    public static void glide(Context context, ImageView iv, String image, BitmapTransformation... transformations) {
        Glide.with(context).load(image).transform(transformations).into(iv);
    }
    public static void glideFile(Context context, ImageView iv, File image, BitmapTransformation... transformations) {
        Glide.with(context).load(image)
                .skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(iv);
    }

    public static void glide(Context context, ImageView iv, int resId) {
        Glide.with(context).load(Integer.valueOf(resId)).into(iv);
    }

    public static void glideWithError(Context context, ImageView iv, String path, int resourceId) {
        Glide.with(context).load(path).error(resourceId).into(iv);
    }

    public static void glide(Context context, final Resources res, final View view, String path) {
        if (!TextUtils.isEmpty(path)) {
            Glide.with(context).load(path).asBitmap().into(new SimpleTarget<Bitmap>() {
                @RequiresApi(api = 16)
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    view.setBackground(new BitmapDrawable(res, resource));
                }
            });
        }
    }

//    public static void glideAutoRotate(Context context, ImageView iv, String path, float rotate, int defaultIco, RequestListener<? super String, GlideDrawable> requestListener, int circleRadian) {
//        if (path != null) {
//            Glide.with(context).load(path).placeholder(defaultIco).listener(requestListener).diskCacheStrategy(DiskCacheStrategy.ALL).transform(new AutoRotateTransformation(context, rotate), new CenterCrop(context), new GlideRoundTransform(context, circleRadian)).into(iv);
//        }
//    }
//
//    public static void glideAutoRotateH(Context context, ImageView iv, String path, float rotate, int defaultIco, RequestListener<? super String, GlideDrawable> requestListener, int circleRadian) {
//        if (path != null) {
//            Glide.with(context).load(path).placeholder(defaultIco).listener(requestListener).diskCacheStrategy(DiskCacheStrategy.ALL).transform(new AutoRotateTransformationH(context, rotate), new CenterCrop(context), new GlideRoundTransform(context, circleRadian)).into(iv);
//        }
//    }
}